package com.logic.landseaserver.ws;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.RefundLog;
import com.logic.landseaserver.service.IRefund;
import com.logic.landseaserver.ws.dto.OffrentContactDTO;
import com.logic.landseaserver.ws.dto.RefundDTO;
import com.logic.landseaserver.ws.dto.RefundLogDTO;
import com.logic.landseaserver.ws.dto.RefundReqDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 *
 * 项目名称：system-server</br>
 * 类名称：RefundRest</br>
 * 类描述：</br>
 * 创建人：tony</br>
 * 创建时间：2017年8月21日 下午4:35:27</br>
 * @version 1.0
 *
 */
@Path("/refund")
@Api(value = "refund", description = "退款 Rest WebServices", position = 42)
public class RefundRest {
    
    private static final Logger logger = LoggerFactory.getLogger(RefundRest.class);
    
    @Autowired
    private IRefund refundServiceImpl;
    
    
    @POST
    @Path("/applyRefund")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "退款申请(保存 & 编辑)")
    public Response applyRefund(
            @ApiParam(value = "param", required = false) RefundReqDTO refunddto) {
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = refundServiceImpl.addOrEditRefund(refunddto);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e){
            LoggerHelper.handleException(logger, "applyRefund", e, map);
        }
        catch (Exception e) {
            logger.error("offRent || applyRefund error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/getRefundList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "退款申请详情")
    public Response getRefund(
        @ApiParam(value = "param", required = true)
        QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<RefundDTO> refundList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            refundList = refundServiceImpl.getRefundList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(refundList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("getRefundList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/confirmRefund")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "审批确认")
    public Response confirmRefund(
        @ApiParam(value = "param", required = true) RefundLog refundlog){
        CommonResultMap map = new CommonResultMap();
        try {
            HashMap<String,Object> result = refundServiceImpl.confirmRefund(refundlog);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            LoggerHelper.handleException(logger, "confirmRefund", e, map);
        }
        catch (Exception e) {
            logger.error("confirmRefund error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/confirmRecord")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "审批记录")
    public Response confirmRecord(
        @ApiParam(value = "param", required = true) @QueryParam("refundId") Integer refundId ){
        CommonResultMap map = new CommonResultMap();
        try {
            List<RefundLogDTO> result = refundServiceImpl.confirmRecord(refundId);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("getRefundList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/checkRefund")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "判断退房账单是否存在退款申请", position = 4)
    public Response checkRefundApply(@ApiParam(value = "paramMap", required = true) Map<String, Object> paramMap)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Integer objectId = (Integer)paramMap.get("objectId");
            Integer objectType =  (Integer)paramMap.get("objectType");
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("result", refundServiceImpl.checkApplyRefund(objectId,objectType));
            map.setData(result);
            map.setResult(CommonResult.SUCCESS, "checkRefundApply success");
        }catch (LandseaException e){
            logger.info("checkRefundApply BusinessException" + e.getMessage());
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getErrorDesc());
        }catch (Exception e){
            logger.info("checkRefundApply Exception" + e.getMessage());
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/createContact")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增退款联系记录")
    public Response createContact( @ApiParam(value = "param", required = false) OffrentContactDTO ocDto) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData( refundServiceImpl.createContact(ocDto));
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            logger.error("offRent || createContact error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("offRent || createContact error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/queryContact/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询退款联系记录")
    public Response queryContact(
            @ApiParam(value = "param", required = true) @PathParam(value="contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(refundServiceImpl.queryContactByContractId(contractId));
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("refund || queryContact error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("refund || queryContact error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/getStatusRole")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询状态条件值")
    public Response getStatusRole()
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            map.setData(refundServiceImpl.getStatusRole());
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            logger.error("refund || getStatusRole error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }   
    
    @GET
    @Path("/printRefundReceipt/{refundId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "打印清退结算单")
    public Response printRefundReceipt(@ApiParam(value = "param", required = true) 
    		@PathParam(value="refundId") Integer refundId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
        	Map<String,Object> data =new HashMap<String,Object>();
        	data = refundServiceImpl.printRefundReceipt(refundId);
            map.setData(data);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            logger.error("printRefundReceipt || 打印清退结算单 error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }  
}


